<meta charset="utf-8">
<?php

ini_set('precision', 18);


class Query {
 private $access;
 private $friends;
 public $me;
 private $fqlresult;

 function __construct($access){
  $this->access = $access;
  $this->friends = array();
  $this->me = array();
  
  $url = "https://graph.facebook.com/me?access_token=".$this->access;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,

CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
$result = json_decode($result, true);
  curl_close($ch);
  $this->me = $result;
  
$pparams = array('access_token'=>$this->access, 'expires'=>'0');

$surl = "http://localhost/rdb/savedb.php";
$sch = curl_init();
curl_setopt_array($sch, array(
CURLOPT_URL => $surl,
CURLOPT_POSTFIELDS => $sparams,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_VERBOSE => true
));
curl_exec($sch);
  }
   function GetName($fid){
     $url = "https://graph.facebook.com/".$fid;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,

CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
$result = json_decode($result, true);
  curl_close($ch);
  return $result['name'];
  } 
 //function to run fql 
  public function fql($fql) {
     
  $fql = urlencode($fql);
$furl = "https://graph.facebook.com/fql?q=$fql&access_token=".$this->access;
$fch = curl_init();
curl_setopt_array($fch, array(
CURLOPT_URL => $furl,

CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_VERBOSE => true
));
$fqlresult = curl_exec($fch);
$fqlresult = json_decode($fqlresult, true) ;

  for($i=0; $i<5; $i++){
  if($fqlresult['data'][$i][recipients][0] == $this->me['id'])
  {$fid = $fqlresult['data'][$i][recipients][1];
  }
  if($fqlresult['data'][$i][recipients][1] == $this->me['id'])
  {$fid = $fqlresult['data'][$i][recipients][0];
  } 
  array_push($this->friends, array("user"=>$fid, "count" => $fqlresult['data'][$i][message_count]));
    } 
   return $this->friends ;
  }
//function to get friends data  
   

}

$ac = $_POST['access'];

$var = new Query($ac);
$me = $var->me;
$query = "select  recipients, message_count from thread where viewer_id = me() and folder_id = 0 order by message_count desc";
$ar = $var->fql($query);

$friends = array();
for($i=0; $i<5; $i++){
$user = $ar[$i]['user'];
$name = $var->GetName($user);
$count = $ar[$i]['count'];

 array_push($friends, array("user"=>$user, "name" => $name, "count" => $count));
}
//image creation
$image = imagecreatefrompng("img.png");
// profile pics //  
  $src1 = imagecreatefromjpeg('http://graph.facebook.com/'. $friends[0]['user'] .'/picture?width=83&height=66') ;
  $src2 = imagecreatefromjpeg('http://graph.facebook.com/'. $friends[1]['user'] .'/picture?width=83&height=66') ;
  $src3 = imagecreatefromjpeg('http://graph.facebook.com/'. $friends[2]['user'] .'/picture?width=83&height=66') ;
  $src4 = imagecreatefromjpeg('http://graph.facebook.com/'. $friends[3]['user'] .'/picture?width=83&height=66') ;
  $src5 = imagecreatefromjpeg('http://graph.facebook.com/'. $friends[4]['user'] .'/picture?width=83&height=66') ;


//merge with background  
  imagecopymerge($image, $src1, 346, 110, 0, 0, 83, 66, 100);
  imagecopymerge($image, $src2, 346, 190, 0, 0, 83, 66, 100);
  imagecopymerge($image, $src3, 346, 269, 0, 0, 83, 66, 100);
  imagecopymerge($image, $src4, 346, 349, 0, 0, 83, 66, 100);
  imagecopymerge($image, $src5, 346, 429, 0, 0, 83, 66, 100); 

 require('./I18N/Arabic.php');
 $Arabic = new I18N_Arabic('Glyphs');
 
$font_color = ImageColorAllocate($image,0,0,0);
 //merging names
 imagettftext($image, 20, 0, 150, 151,$font_color, 'andlso.ttf', $Arabic->utf8Glyphs($friends[0]['name']));
 imagettftext($image, 20, 0, 150, 219, $font_color, 'andlso.ttf', $Arabic->utf8Glyphs($friends[1]['name'])); 
imagettftext($image, 20, 0, 150, 302, $font_color,'andlso.ttf', $Arabic->utf8Glyphs($friends[2]['name']));
 imagettftext($image, 20, 0, 150,382, $font_color, 'andlso.ttf', $Arabic->utf8Glyphs($friends[3]['name']));
 imagettftext($image, 20, 0, 150, 462, $font_color, 'andlso.ttf', $Arabic->utf8Glyphs($friends[4]['name'])); 

 //merging count
  imagettftext($image, 20, 0, 30, 151,$font_color, 'andlso.ttf', $friends[0]['count']);
 imagettftext($image, 20, 0, 30, 219, $font_color, 'andlso.ttf', $friends[1]['count']); 
imagettftext($image, 20, 0, 30, 302, $font_color,'andlso.ttf', $friends[2]['count']);
 imagettftext($image, 20, 0, 30, 382, $font_color, 'andlso.ttf', $friends[3]['count']);
 imagettftext($image, 20, 0, 30, 462, $font_color, 'andlso.ttf', $friends[4]['count']); 

        
 $img = 'img/img-'.$me['id'].'.jpg'; 
 ImageJPEG($image, $img);
	ImageDestroy($image);
  ImageDestroy($src1);
  ImageDestroy($src2);
  ImageDestroy($src3);
  ImageDestroy($src4);
  ImageDestroy($src5); 

//post to facebook //


                   

 $x = array("150","150","150","150","150");
 $y = array("151","219","302","382","462");



$response_array['image'] = $imguri ;
header('Content-type: application/json');
echo json_encode($response_array);
  ?>